package importPool;

import java.io.RandomAccessFile;
import java.sql.ResultSet;
import java.util.Hashtable;
import java.util.Vector;

import pool.bean.Master_table_Bean;

import control.RequestParameter;

import dbmanager.ChekInMaster;
import dbmanager.DBManager;


public class RegisterMaster 
{
	
	private String Path;
	DBManager db = new DBManager();
	private RequestParameter reqparam;
	
	private String responsePath  ;
	
	public RegisterMaster(String Path,String response) 
	{
		this.Path = Path;
		this.responsePath = response;
		db.getConnect();
	}
	
	
	
	public void registerItem(Vector<Object> indexVector)
	{
		
		String Property=" ";
		int PropertyCol;		
		Hashtable<Object,Object> propertyHash = new Hashtable<Object, Object>();
		
		setRequestParameter();	
		Master_table_Bean mtb ;
		try 
		{
			RandomAccessFile raf = new RandomAccessFile(Path,"rws");
			RandomAccessFile ItemReqResp = new RandomAccessFile(responsePath,"rws");
			
			String line = null;
			line=raf.readLine();
			String[] column = line.split("\t");
			
			ChekInMaster checkinmaster = new ChekInMaster(db);
			
			for(int i=0;i<indexVector.size();i++)
			{
				String strIndex = indexVector.get(i).toString();
				int index = Integer.parseInt(strIndex);
				String masterObject = column[index];
				masterObject = masterObject.trim();
				if(!checkinmaster.isInMaster(masterObject))
				{	
					mtb = new Master_table_Bean();
					mtb.setDBManager(db);
					mtb.setReqParam(reqparam);
					mtb.setNewHashtable(masterObject);
					
					propertyHash = new Hashtable<Object, Object>();
					propertyHash.put("pid","type");
					propertyHash.put("pv","attribute");
					
					mtb.setPropertyHash(propertyHash);					
					mtb.insert();
				}
			}
			while((line=raf.readLine())!=null)
			{					
				String records[] = null;
				try 
				{
					records = line.split("\t");
					String mastername = records[0].trim();
					if(mastername.equals(""))
					{
						break;
					}
					try
					{
						ResultSet rs1 = db.getSelect("select * from master_table where name like '%"+mastername+"%'");
						int countCheck =0;
						while(rs1.next())
							countCheck++;
						if(countCheck>0)
						mastername = mastername+"_"+countCheck;
						records[0] = mastername;						
					} catch (Exception e1){
						e1.printStackTrace();
					}
					
					ItemReqResp.seek(ItemReqResp.length());
					ItemReqResp.writeBytes("\nRequest :"+mastername+" ");
					
					mtb = new Master_table_Bean();
					mtb.setDBManager(db);
					mtb.setReqParam(reqparam);					
					mtb.setNewHashtable(mastername.trim());
					
					for(int k=1;k<indexVector.size();k++)
					{
						Property= indexVector.get(k).toString();
						PropertyCol=Integer.parseInt(Property);
						
						propertyHash = new Hashtable<Object, Object>();	
						propertyHash.put("pid",column[PropertyCol].trim());
						propertyHash.put("pv",records[PropertyCol].trim());						
						mtb.setPropertyHash(propertyHash);			
						ItemReqResp.writeBytes(propertyHash.toString());
					}
					
					ItemReqResp.writeBytes("\n");
					StringBuffer strbfr = new StringBuffer();
					strbfr.append(mtb.insert());
					
					ItemReqResp.writeBytes("Response :"+"\n"+strbfr+"\n"+"\n");
				}
				catch (Exception e) {
					e.printStackTrace();
				}				
			}			
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void setRequestParameter()
	{
		reqparam = new RequestParameter();
		
		reqparam.setUsername("+911234567891");
		reqparam.setMessage("System Message");
		reqparam.setPassword("891");
		reqparam.setRequest("master_table");
		reqparam.setProcess("add");			
	}
	
	
	/*public void register(Vector<Object> indexVector)
	{
			String Property=" ";
			int PropertyCol;
			int Count=0;
			try 
			{
				RandomAccessFile ItemOut = new RandomAccessFile("/home/wiki/Desktop/ItemList/itemop","rws");
				ItemReqResp = new RandomAccessFile("/home/wiki/Desktop/ItemList/ItemReqResp","rws");
			
				//	String ReqResPath=Input.ReqResPATH++ Input.FILRFORMAT;
				//ItemReqResp = new RandomAccessFile("ReqResPath","rws");
				
				RandomAccessFile raf = new RandomAccessFile(Path,"rws");
				//RandomAccessFile log = new RandomAccessFile("/home/wiki/Desktop/ItemList/Request.txt","rws");
				RandomAccessFile Error = new RandomAccessFile("/home/wiki/Desktop/ItemList/Error.txt","rws");
				Error.seek(Error.length());
				//log.seek(log.length());
				String line = null;
				while((line=raf.readLine())!=null)
				{
					String[] col = line.split("\t");
					
					if(ItemOut.length()==0)
						ItemOut.writeBytes(line+"\tItemcode\n");
					else
						ItemOut.seek(ItemOut.length());
					
					ResultSet rs = db.getSelect("SELECT * FROM command where lower(fieldname)='name' and msgtype = 'master_table' and process='add'");
					rs.next();
					String NameChar = rs.getString("spchar");
					
					rs = db.getSelect("SELECT * FROM command where lower(fieldname)='pid' and msgtype = 'property_details' and process='add'");
					rs.next();
					String pidChar = rs.getString("spchar");
					
					rs = db.getSelect("SELECT * FROM command where lower(fieldname)='pv' and msgtype = 'property_details' and process='add'");
					rs.next();
					String pvChar = rs.getString("spchar");
					//String userid = "systemuser";
					//Uservalidation getpass = new Uservalidation(db);
					//String password = getpass.getPasword(userid);
					String splitline[] = line.split("\t");
					String request = "11 master_table add" +NameChar;
					ChekInMaster checkinmaster = new ChekInMaster(db);
					
					for(int i=0;i<indexVector.size();i++)
					{
						Property= indexVector.get(i).toString();
						PropertyCol=Integer.parseInt(Property);
						if(!checkinmaster.isInMaster(splitline[i]))
						{
							String mastermsg =  request + "\"" + splitline[i] + "\"" + pidChar + "type" + pvChar + "other"+"#" + "605";
							Control controlClassObj = new Control("+919922918605",mastermsg,db);
							controlClassObj.messageProcessing().toString();
							controlClassObj.requestProcess();
						}
					}
					while((line=raf.readLine())!=null)
					{
						String message ="";
						String records[] = null;
						try 
						{
							records = line.split("\t");
							String mastername = records[0].trim();
							if(mastername.equals(""))
							{
								break;
							}
							try
							{
								ResultSet rs1 = db.getSelect("select * from master_table where name like'%"+mastername+"%'");
								int countCheck =0;
								while(rs1.next())
									countCheck++;
								if(countCheck>0)
								mastername = mastername+"_"+countCheck;
								records[0] = mastername;
								
							} catch (Exception e1) 
							{
								e1.printStackTrace();
							}
							message = NameChar+"\""+mastername+"\"";
							String propString ="";
							for(int k=1;k<indexVector.size();k++)
							{
								Property= indexVector.get(k).toString();
								PropertyCol=Integer.parseInt(Property);
								try
								{
									String pv = records[PropertyCol].trim();
									if(pv.length()!=0)
										propString = propString+pidChar+"\""+col[PropertyCol].trim()+"\""+pvChar+"\""+pv+"\"";
								}
								catch (Exception e) 
								{
									
								}
							}
							System.out.println(propString);
							message = message + propString;
							System.out.println(message);
							message = "11 master_table add"+message+"#"+"605";
							Request=message;
							ItemReqResp.seek(ItemReqResp.length());
							ItemReqResp.writeBytes("\nRequest :"+"\n"+Request+"\n");
							//log.writeBytes(message+"\n");
							System.out.println(message);
							Control controlClassObj = new Control("+919922918605",message,db);
							controlClassObj.messageProcessing().toString();
							String error = controlClassObj.requestProcess().toString();
							ItemReqResp.writeBytes("Response :"+"\n"+error+"\n"+"\n");
							if(error.toLowerCase().contains("error")==false)
							{						
								Itemcodegenerator itemcode = new Itemcodegenerator();
							//	itemcode.SetData(message,"", "", "+919922918605");
								itemcode.initializationData(db);
								itemcode.setObject("");
								System.out.println("Result is : "+itemcode.getResult());
								ItemOut.writeBytes(mastername+"\t");
								for(int j=1;j<records.length;j++)
								{
									ItemOut.writeBytes(records[j]+"\t");
								}
								ItemOut.writeBytes(itemcode.getItemCode()+"\n");
								Count++;
							}
							else
								Error.writeBytes(message+"\n");
						} 
						catch (Exception e) 
						{
							Error.writeBytes(message+"\n");
						}
					}
					//log.close();
					Error.close();
				} 
			}
			catch (Exception e) 
			{
				e.printStackTrace();
			}
	}*/
	
	public static void main(String[] args) 
	{
		String path = ImportFilePath.INPUTPATH+"Item.txt";
		String responsePath = ImportFilePath.MASTERRESPONSEPATH;
		RegisterMaster reg = new RegisterMaster(path,responsePath);
		Vector<Object> indexVector=new Vector<Object>();
		indexVector.add(0);
		indexVector.add(4);
		indexVector.add(5);
		reg.registerItem(indexVector);
	}
}
